/**
 * @author Bachelor
 */
public class Main {
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        // write your code here
        int[] arr = {5, 3, 6, 8, 1, 7, 9, 4};
        for (int i = 0; i < arr.length / 2; i++) {
            int minIndex = i;
            int maxIndex = arr.length - 1 - i;

            for (int j = i + 1; j <= arr.length - 1 - i; j++) {
                // 找出arr[i...n)中最小值的索引
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
                // 找出arr[i...n)中最大值的索引
                if (arr[j] > arr[maxIndex]) {
                    maxIndex = j;
                }
            }
            // 交换两数
            swap(arr, i, minIndex);
            if (maxIndex == i) {
                continue;
            }
            swap(arr, arr.length - 1 - i, maxIndex);
        }
        for (int e : arr) {
            System.out.print(e + " ");
        }
    }
}
